/**
 * @class yijs.List
 * Copyright (c) 2009-2009 谢文浩
 * @fileOverview 创建一个列表
 * @author 谢文浩
 * @email mohaiguyan12@126.com
 * @version 0.22
 * @date 2009-11-11
 */

/**
 * @constructor
 * @param {Object} options
 */
yijs.List = function(options){
	//组件的默认参数
	var defaults = {
		/** 是否自动运行组件（如果为false，请在实例化组件类后，调用render（）方法）*/
		autoRender : false,
		/** 给List添加id*/
		id : null,
		/** 用来容纳组件的容器*/
		renderTo : null,
		/**应用List的对象*/
		applyTo : null,
		/** 数据源*/
		dataProvider : null,
		/** 宽度*/
		width: null,
		/** 高度自动延伸*/
		autoHeight : true,
		/** 高度*/
		height: null,
		/** 是否允许操作组件*/
		enabled : true,
		/** 给组件添加样式*/
		style : null,
		/** 列表清单中显示的字段*/
		labelField : null,
		/** 是否显示滚动条*/
		isShowScrollBar : false,
		/** 自定义List元素显示的数据*/
		itemRender : null,
		/** 是否开启多选*/
		multi : false,
		/** 定位*/
		position : 'static',
		/** 等同于css中的top（position为absolute时，才有效）*/
		top : 0,
		/** 等同于css中的left（position为absolute时，才有效）*/
		left : 0,
		/** 组件中所用到的class*/
		cls : {
			/** 鼠标经过子项时的样式*/
			itemHover : null,
			/** 子项的图标样式*/
			itemIcon : null,
			/** 子项被点击后的样式*/
			itemSelect : null
		},
		/**事件监听*/
		listeners : {
			render : null,
			create : null,
			itemMouseover : null,
			itemMouseout : null,
			/** 子项被点击后触发的事件*/
			itemClick : null,
			/** 子项被双击后触发的事件*/
			itemDblclick : null,
			/** 添加新的子项后触发的事件*/
			addItem : null,
			/** 删除子项后触发的事件*/
			removeItem : null,
			/** 删除所有子项时后触发的事件*/
			removeAll : null,
			/** 当组件显示后触发的事件*/
			show : null,
			/** 当组件隐藏后触发的事件*/
			hide : null,
			/** 子项被选中后触发的事件*/
			select : null,
			/** 子项移除选中后触发的事件*/
			removeSelect : null
		}
	}; 	
	this.cls = {
			itemHover : "yijs_list_itemHover",
			itemIcon : null,
			itemSelect : 'yijs_list_itemSelect'			
	}
	this.cls = $.extend(this.cls,options.cls);	
	this.options = $.extend(defaults, options);	
	this.listeners = this.options.listeners;
	this.$applyTo = null;
	this.$list = null;
	this.$renderTo = null;
	this.length = 0;
	this.selectData = null;
	this.selectsData = [];
	if(this.options.autoRender) this.render();		
}
yijs.extend(yijs.List,yijs.Ui);
yijs.List.prototype.render = function(){
		/**
		 * 运行组件
		 */
		var _opts = this.options;
		this.$renderTo = _opts.renderTo != null && $(_opts.renderTo) || null;
		if(this.$renderTo != null && this.$renderTo.size()>0 && _opts.dataProvider != null){
			_opts.dataProvider.uis.push(this);
			var $list = this.$renderTo.children('.yijs_list');
			$list.size() > 0 ? this._reCreate() : this._create();
		}
		var event = {target : this};
		this.listeners.render != null && this.listeners.render.call(this,event);			
}
/**
 * 创建list
 */
yijs.List.prototype._create = function(){
	var _opts = this.options;
	//向容器添加ul
	var list = "<ul class='yijs_list'></ul>";
	this.$renderTo.append(list);
	this.$list = this.$renderTo.children('.yijs_list');
	_opts.id != null && this.$list.attr("id",_opts.id);
	_opts.width != null && this.$list.width(_opts.width);
	_opts.style != null && this.$list.css(_opts.style);
	!_opts.isShowScrollBar && this.$list.css('overflow','hidden');
	!_opts.autoHeight && _opts.height != null && this.$list.height(_opts.height);
	_opts.position == 'absolute' && this.$list.css({'position':'absolute','top': parseInt(_opts.top)+'px','left':parseInt(_opts.left) + 'px'});
	this._proccessData();
	var event = {target : this};
	this.listeners.create != null && this.listeners.create.call(this,event);		
	
}
yijs.List.prototype._proccessData = function(){
	//处理数据
	var _opts = this.options;
	var _data = _opts.dataProvider.source;
	if(_data.length > 0){
		for(var i = 0;i<_data.length;i++){
			this.addItem(_data[i]);
		}
	}
	this.length = _data.length;	
}
/**
 * 重新创建List
 */
yijs.List.prototype._reCreate = function(){
	this.removeAll();
	this._proccessData();
}
/**
 * 选中指定索引值的项
 * @param {number | array} index
 */
yijs.List.prototype.select = function(index){
		var _that = this;
		var _opts = this.options;
		var itemSelectCls = _that.cls.itemSelect;
		if(typeof index == 'object' && index.length > 0){
			$.each(index,function(i){
				_select(Number(index[i]));
			})
		}else{
			_select(index);
		}
		
		data = !_opts.multi && _that.selectData || _that.selectsData;
		var event = {target : this,index : index,data:data};
		this.listeners.select != null && this.listeners.select.call(this,event);
		
		function _select(index){
			var $select = _that.$list.children('li').eq(index);
			$select.addClass(itemSelectCls);
			!_opts.multi && $select.siblings().removeClass(itemSelectCls);	
			var data = _opts.dataProvider.source[index];
			if(!_opts.multi){
				_that.selectData = data;
			}else{
				data.index = index;
				 _that.selectsData.push(data);
			}			
		}		
}
/**
 * 删除选中状态
 * @param {number | array} index
 */
yijs.List.prototype.removeSelect = function(index){
		var _that = this;
		var _opts = this.options;
		var itemSelectCls = _that.cls.itemSelect;
		if(typeof index == 'object' && index.length > 0){
			$.each(index,function(i){
				_removeSelect(Number(index[i]));
			})
		}else{
			_removeSelect(index);
		}
		
		function _removeSelect(index){
			var $select = _that.$list.children('li').eq(index);
			$select.removeClass(itemSelectCls);	
			if(!_opts.multi){
				_that.selectData = null;
			}else{
				var _arr = [];
				for(var i = 0;i<_that.selectsData.length;i++){
					index != Number(_that.selectsData[i].index) && _arr.push(_that.selectsData[i]);
				}
				_that.selectsData = _arr;				
			}						
		}	
}
/**
 * 添加子元素
 * @param {Object|string} item
 */
yijs.List.prototype.addItem = function(item){
	var _listLi = this._getListLi(item);
	this.$list.append(_listLi);
	var $currentLi = this.$list.children("li:last");
	this._afterAdd($currentLi,this.length);
}
/**
 * 添加子项到指定索引
 * @param {number} index
 * @param {Object|string} item
 */
yijs.List.prototype.addItemAt = function(index,item){
	var $indexLi = this.$list.children("li").eq(index);
	var _listLi = this._getListLi(item);
	$indexLi.before(_listLi);
	var $currentLi = $indexLi.prev();
	this._afterAdd($currentLi,index);	
}
/**
 * 添加多条数据
 * @param {array} items
 */
yijs.List.prototype.addAll = function(items){
	if(items.length > 0){
		for(var i=0;i<items.length;i++){
			this.addItem(items[i]);
		}
	}
}
/**
 * 插入多条数据到指定索引
 * @param {number} index
 * @param {array} items
 */
yijs.List.prototype.addAllAt = function(index,items){
	if(items.length > 0){
		for(var i=0;i<items.length;i++){
			this.addItemAt(index,items[i]);
		}
	}	
}
/**
 * 获取子项的html代码
 * @param {Object|string} item
 */
yijs.List.prototype._getListLi = function(item){
	var _itemRender = this.options.itemRender;
	var icon = this._getIcon(item);
	if(_itemRender != null) item = _itemRender.call(this,item);
	return typeof item == "string" && _getLi(item) || typeof item == "object" && _getLi(item[this.options.labelField])|| "";	
	
	function _getLi(item){
		var clear = !$.browser.msie &&  "<div class='clear' />" || '';
		return "<li class='yijs_list_li'>"+icon+"<span>"+item+"</span>"+clear+"</li>";
	}	
}
/**
 *添加图标 
 */
yijs.List.prototype._getIcon = function(item){
	var _opts = this.options;
	if(_opts.iconClsField != null || this.cls.itemIcon){
		var iconCls = this.cls.itemIcon  || item[_opts.iconClsField] || '';

		var icon = "<span class='yijs_List_icon "+iconCls+"'></span>";
		return icon;
	}
	return "";
}
/**
 * 在添加后执行方法
 * @param {Object} $currentLi
 */
yijs.List.prototype._afterAdd = function($currentLi,index){
	var _that = this;
	var _opts = this.options;
	var itemHoverCls = this.cls.itemHover;
	$currentLi.hover(function(){$(this).addClass(itemHoverCls);
		var event = {target : _that,that : this};
		_that.listeners.itemMouseover != null && _that.listeners.itemMouseover.call(this,event);	
	},function(){$(this).removeClass(itemHoverCls);
		var event = {target : _that,that : this};
		_that.listeners.itemMouseout != null && _that.listeners.itemMouseout.call(this,event);	
	});	
	//给li绑定单击事件
	$currentLi.click(function(){
		var index = _that.$list.children("li").index($(this));
		$(this).hasClass(_that.cls.itemSelect) ?  _that.removeSelect(index) : _that.select(index);
		
		var data = _that.options.dataProvider.source[index];
		var event = {target : _that,index : index,data:data};
		_that.listeners.itemClick != null && _that.listeners.itemClick.call(this,event);		
	})
	//给li绑定双击事件
	$currentLi.dblclick(function(){
		var index = _that.$list.children("li").index($(this));
		var event = {target : _that,index : index,data:_that.options.dataProvider.source[index]};
		_that.listeners.itemDblclick != null && _that.listeners.itemDblclick.call(this,event);		
	})
	this.length ++;
	
	var event = {target : this,index : index,data:this.options.dataProvider.source[index]};
	this.listeners.addItem != null && this.listeners.addItem.call(this,event);
	
}
/**
 * 删除指定索引的子项
 * @param {number} index
 */
yijs.List.prototype.removeItemAt = function(index){
	var $indexLi = this.$list.children("li").eq(index);
	$indexLi.size() > 0 && $indexLi.remove() && this.length --;
	
	var event = {target : this,index : index,data:this.options.dataProvider.source[index]};
	this.listeners.removeItem != null && this.listeners.removeItem.call(this,event);	
}
/**
 * 删除所有数据
 */
yijs.List.prototype.removeAll = function(){
	this.$list.html("");
	this.length = 0;
	
	var event = {target : this};
	this.listeners.removeAll != null && this.listeners.removeAll.call(this,event);		
}


/**
 * 获取指定索引值的数据
 * @param {number} index
 */
yijs.List.prototype.getItemAt = function(index){
	return this.options.dataProvider.source[index];
}

/**
 * 修改指定索引值的数据
 * @param {number} index
 * @param {Object | string} item
 */
yijs.List.prototype.setItemAt = function(index,item){
	var $indexLi = this.$list.children("li").eq(index);
	typeof 	item == "string" && $indexLi.html(item) || typeof 	item == "object" && this.options.labelField != null && $indexLi.html(item[this.options.labelField]);
	
	var event = {target : this,index : index,data:this.options.dataProvider.source[index]};
	this.listeners.setItem != null && this.listeners.setItem.call(this,event);		
}
/**
 * 获取List所包含的子项数量
 */
yijs.List.prototype.size = function(){
	return this.$list.children("li").size();
}
/**
 * 显示列表
 */
yijs.List.prototype.show = function(){
	this.$list.css("display","none") && this.$list.fadeIn('fast');
	
	var event = {target : this};
	this.listeners.show != null && this.listeners.show.call(this,event);		
}
/**
 * 隐藏列表
 */	
yijs.List.prototype.hide = function(){
	this.$list.css("display","block") && this.$list.fadeOut('fast');

	var event = {target : this};
	this.listeners.hide != null && this.listeners.hide.call(this,event);	
}
